package com.api.gbhg.huangwm.BxActionV2;

import com.customization.commons.LogTool;
import weaver.conn.RecordSet;
import weaver.general.GCONST;
import weaver.hrm.User;

import java.util.List;
import java.util.Map;

public class test {
    public static void main(String[] args) {
        String rootPath = "D:\\WEAVER\\ecology\\";
        GCONST.setRootPath(rootPath);
        GCONST.setServerName("ecology");

        test test = new test();
        test.test();
    }
    private LogTool logTool = new LogTool("/log/gbhg/huangwm/BxActionV2/FrozenQuotaForAuditDetailsV2",false);
    private BxActionUtils utils = new BxActionUtils();
    public void test(){
        String dtTable_0 = "formtable_main_905_dt1";
        int billid = 13497;
        int requestid = 2192534;
        RecordSet rs = new RecordSet();
        //第一步 支出渠道为财政拨款时检查选择的渠道中的预算来源跟表单中的预算来源是否一致
        String sql1 = "select * from "+dtTable_0+" where  mainid="+billid;
        rs.execute(sql1);
        int i=1;
        while (rs.next()){
            int zcqd = rs.getInt("zcqd");
            //财政拨款
            if(zcqd==0){
                int ysly = rs.getInt("ysly");
                int xzkmdm = rs.getInt("xzkmdm");//财政拨款项目
                if(xzkmdm<=0){
                    System.err.println(
                            "第"+i+"行报销信息为财拨支出时未获取到财政拨款项目"
                    );
                    return;
                }
                Map<String,String> qudao = utils.getQudaoYsly(xzkmdm);
                int zfqd = Integer.parseInt(qudao.get("zfqd"));
                if(zfqd!=ysly){
                    System.err.println(
                            "第"+i+"行报销信息为财拨支出时渠道中的预算来源与所选预算来源不一致.渠道中为"+(zfqd==0?"当年预算":"上年结转")+" 审核选择为"+(ysly==0?"当年预算":"上年结转")
                    );
                    return;
                }
            }
            i++;
        }

        //第二步 校验支出渠道的剩余金额
        Map<String,Object> check = utils.checkAuditDetailsToQudao(dtTable_0,billid);
        if(!(boolean)check.get("status")){
            System.err.println(check.get("msg").toString());
            return;
        }

        //经济分类的数据
        List<Map<String,Object>> jjfldatas = utils.getDetailsInfo_checkAudit(dtTable_0,billid);
        //第三步 根据同意报销金额跟申请报销金额的差额重新判断jingfei表额度
        RecordSet rs2 = new RecordSet();
        logTool.writeLog("为了防止同意报销金额比申请报销金额大重新判断jingfei表剩余额度");
        for(int j=0;i<jjfldatas.size();j++){
            Map<String,Object> data = jjfldatas.get(j);
            String ysxm = data.get("ysxm").toString();
            String bmjjzcfl = data.get("bmjjzcfl").toString();
            String chae = data.get("chae").toString();
            String sql = "select lcjlid,xmmc,bmzcjjfl,syed,(syed-("+chae+"))as result from uf_jingfei where lcjlid="+ysxm+" and bmzcjjfl="+bmjjzcfl;
            logTool.writeLog(sql);
            if(rs2.execute(sql) && rs2.next()){
                Double thisResult = rs2.getDouble("result");
                String xmmc = rs2.getString("xmmc");
                if(thisResult<0){
                    System.err.println(
                            "报销项目["+xmmc+"]中的经济分类["+bmjjzcfl+"]剩余额度["+thisResult+"]不足以扣减同意报销金额差额:"+chae
                    );
                    return;
                }
            }else {
                System.err.println(
                        "报销项目["+utils.getProjectInfo(ysxm,"xmmin")+"]中的经济分类["+bmjjzcfl+"]未查询到经费记录"
                );
                return;
            }
        }

        //用于使用单位额度操作的数据
        List<Map<String,Object>> ysxmdatas = utils.getDetailsInfo_checkAuditSyeded(dtTable_0,billid);
        User user = new User(11759);
        int subid = user.getUserSubCompany1();
        //第四步 根据差额校验项目使用单位额度
        RecordSet rs3 = new RecordSet();
        logTool.writeLog("为了防止同意报销金额比申请报销金额大重新判断项目使用单位剩余额度");
        for(int j=0;i<jjfldatas.size();j++){
            Map<String,Object> data = jjfldatas.get(j);
            String ysxm = data.get("ysxm").toString();
            String chae = data.get("chae").toString();
            String sql = "select isnull(djje,0)as djje,syed,ysyed,(seyd-("+chae+"))as result from uf_ysxmsydwfped where xmbzlc=" + ysxm + " and sydw=" + subid;
            logTool.writeLog(sql);
            if(rs3.execute(sql) && rs3.next()){
                Double thisResult = rs3.getDouble("result");;
                if(thisResult<0){
                    System.err.println(
                            "报销项目["+utils.getProjectInfo(ysxm,"xmmin")+"]中使用单位剩余额度["+thisResult+"]不足以扣减同意报销金额差额:"+chae
                    );
                    return;
                }
            }else {
                System.err.println(
                        "报销项目["+utils.getProjectInfo(ysxm,"xmmin")+"]未查询到使用单位的额度分配记录(或查询出错) audit"
                );
                return;
            }
        }

        logTool.writeLog("报销单requestid:"+requestid+" 会计审核校验渠道额度成功");
        return;
    }
}
